package com.sales.controller;

import com.sales.dao.ProfileDao;
import com.sales.entity.Profile;
import com.sales.excption.SalesException;
import com.sales.service.ProfileService;
import com.sales.service.UserService;
import com.sales.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * Created by lihu on 2017/9/7.
 */

@Slf4j
@RestController
@RequestMapping(value = "/api/v1/sales", produces = "application/json;charset=utf-8")
public class ReferralCodeController {


    @Autowired
    ProfileService profileService;

    @Autowired
    UserService userService;
    @Autowired
    ProfileDao profileDao;


    /**
     * 获取我的向关联的邀请码
     *
     * @param principal
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/my-referral-codes")
    public ResponseEntity myReferralCodes(@PageableDefault(value = 15, sort = {"id"}, direction = Sort.Direction.DESC)
                                                  Pageable pageable,
                                          Principal principal) {
        try {
            return new ResponseEntity(profileService.getReferralCodes(principal.getName(), pageable), HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("myReferralCodes is error=>principal:{}", principal, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("myReferralCodes is error=>principal:{}", principal, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 创建邀请码
     *
     * @param principal
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/my-referral-codes")
    public ResponseEntity createReferralCodes(@RequestBody String mobile,
                                              Principal principal) {
        try {

            if (!ValidatorUtil.isMobile(mobile)) {
                log.info("【手机号码有误】mobile:{}", mobile);
                return new ResponseEntity("手机号码有误", HttpStatus.BAD_REQUEST);
            }
            userService.createUser(mobile, principal.getName());
            return new ResponseEntity(HttpStatus.OK);
        } catch (SalesException ex) {
            log.error("createReferralCodes is error=>principal:{}", principal, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("createReferralCodes is error=>principal:{}", principal, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 邀请码启用禁用
     *
     * @param code
     * @param enabled
     * @param principal
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/my-referral-codes/{code}")
    public ResponseEntity updateCode(@PathVariable String code,
                                     @RequestParam(value = "enabled") boolean enabled,
                                     Principal principal) {
        profileService.updateStatus(code, enabled);
        return new ResponseEntity(HttpStatus.OK);
    }


    /**
     * 更新推荐码次数
     *
     * @param count
     * @param refCode
     * @return
     */
    @GetMapping("updateCode")
    @Transactional
    public ResponseEntity updateRefCode(@RequestParam("count") int count, @RequestParam("refCode") String refCode, Principal principal) {
        try {
            profileService.updateRefCode(refCode, count, principal.getName());
        } catch (SalesException e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity("系统运行出错", HttpStatus.SERVICE_UNAVAILABLE);
        }
        return new ResponseEntity("修改成功", HttpStatus.OK);
    }


}
